package org.example.ttms.entityClass.train;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import org.example.ttms.Main;
import org.example.ttms.customize.Pop_ups;

import java.io.*;
import java.util.*;

public class Route{
    private int routeId;
    private String startStation;
    private String endStation;
    private List<String> stationList;
    private int stationNum;
    private int tripsNum;
    private List<String> tripsList = new ArrayList<>();

    public Route() {
    }

    public Route(String startStation, String endStation, List<String> stationList) {
        this.startStation = startStation;
        this.endStation = endStation;
        this.stationList = stationList;
        this.routeId = obtainEndRouteId()+1;
        this.stationNum = stationList.size();
        this.tripsNum = 0;
    }

    public IntegerProperty routeIdProperty(){
        return new SimpleIntegerProperty(routeId);
    }

    public StringProperty startStationProperty(){
        return new SimpleStringProperty(startStation);
    }

    public StringProperty endStationProperty(){
        return new SimpleStringProperty(endStation);
    }

    public IntegerProperty stationNumProperty(){
        return new SimpleIntegerProperty(stationNum);
    }

    public IntegerProperty tripsNumProperty(){
        return new SimpleIntegerProperty(tripsNum);
    }

    public int getRouteId() {
        return routeId;
    }

    public void setRouteId(int routeId) {
        this.routeId = routeId;
    }

    public String getStartStation() {
        return startStation;
    }

    public void setStartStation(String startStation) {
        this.startStation = startStation;
    }

    public String getEndStation() {
        return endStation;
    }

    public void setEndStation(String endStation) {
        this.endStation = endStation;
    }

    public List<String> getStationList() {
        return stationList;
    }

    public void setStationList(List<String> stationList) {
        this.stationList = stationList;
    }

    public int getStationNum() {
        return stationNum;
    }

    public void setStationNum(int stationNum) {
        this.stationNum = stationNum;
    }

    public int getTripsNum() {
        return tripsNum;
    }

    public void setTripsNum(int tripsNum) {
        this.tripsNum = tripsNum;
    }

    public List<String> getTripsList() {
        return tripsList;
    }

    public void setTripsList(List<String> tripsList) {
        this.tripsList = tripsList;
    }

    //获取所有路线
    public HashMap<Integer, Route> obtainAllRoute(){
        HashMap<Integer, Route> hashMap = new HashMap<>();
        String file = "src/main/java/org/example/ttms/file/route.txt";
        try {
            Scanner scan = new Scanner(new FileReader(file));
            while(scan.hasNextLine()){
                String str = scan.nextLine();
                ObjectMapper objectMapper = new ObjectMapper();
                Route route = objectMapper.readValue(str, Route.class);
                hashMap.put(route.routeId, route);
            }
        } catch (FileNotFoundException | JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        return hashMap;
    }
    //获取最后一个路线的ID
    public int obtainEndRouteId(){
        HashMap<Integer, Route> hashMap = obtainAllRoute();
        int id = 0;
        for(Map.Entry<Integer, Route> map : hashMap.entrySet()){
            id = map.getKey();
        }
        return id;
    }
    //判断路线是否存在
    public boolean routeIsExist(Route route){
        HashMap<Integer, Route> hashMap = obtainAllRoute();
        for(Map.Entry<Integer, Route> map : hashMap.entrySet()){
            if(map.getValue().stationNum == route.stationNum &&
            map.getValue().startStation.equals(route.startStation) &&
            map.getValue().endStation.equals(route.endStation)){
                int count = 0;
                for(int i = 0; i < route.stationList.size(); i++){
                    if(route.stationList.get(i).equals(map.getValue().stationList.get(i)))
                        count++;
                }
                if(count == route.stationList.size())
                    return true;
            }
        }
        return false;
    }
    //添加路线评判
    public boolean addRouteJudgement(Route route){
        Pop_ups popUps = new Pop_ups();
        if(routeIsExist(route)){
            popUps.pop_ups3("该路线已存在");
            return false;
        }
        popUps.pop_ups4("添加成功");
        return true;
    }
    //添加路线
    public void addRoute(Route route){
        if(!addRouteJudgement(route))
            return ;
        ObjectMapper objectMapper = new ObjectMapper();
        String file = "src/main/java/org/example/ttms/file/route.txt";
        try {
            String jsonString = objectMapper.writeValueAsString(route);
            System.out.println("addRoute:"+jsonString);
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter
                    (new FileOutputStream(file, true)));
            writer.write(jsonString+"\n");
            writer.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        //路线中的站点都增加一条路线
        Station station = new Station();
        HashMap<String, Station> hashMap = station.obtainAllStation();
        for(String str : stationList){
            hashMap.get(str).setRouteNum(hashMap.get(str).getRouteNum()+1);
        }
        ObservableList<Station> observableList = FXCollections.observableArrayList(hashMap.values());
        station.storageStation(observableList);
    }
    //储存所有路线---覆盖
    public void storageRoute(ObservableList<Route> observableList){
        ObjectMapper objectMapper = new ObjectMapper();
        String file = "src/main/java/org/example/ttms/file/route.txt";
        try {
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(file)));
            String jsonString = null;
            for(Route route : observableList){
                jsonString = objectMapper.writeValueAsString(route);
                writer.write(jsonString+"\n");
            }
            writer.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    //删除指定编号的路线
    public void delRoute(int routeId){
        HashMap<Integer, Route> hashMap = obtainAllRoute();
        hashMap.remove(routeId);
        ObservableList<Route> observableList = FXCollections.observableArrayList(hashMap.values());
        storageRoute(observableList);
    }
    //获取指定编号的路线
    public Route obtainRoute(int routeId){
        HashMap<Integer, Route> hashMap = obtainAllRoute();
        return hashMap.get(routeId);
    }
    //减去该路线的所有站点的路线数
    public void decreaseRouteNum(Route route){
        Station station = new Station();
        HashMap<String, Station> hashMap = station.obtainAllStation();
        for(String stationName : route.stationList){
            hashMap.get(stationName).setRouteNum(hashMap.get(stationName).getRouteNum()-1);
        }
        ObservableList<Station> observableList = FXCollections.observableArrayList(hashMap.values());
        station.storageStation(observableList);
    }
    //返回包含指定所有站点的路线
    public List<Route> obtainContainsRoute(List<String> list){
        HashMap<Integer, Route> hashMap = obtainAllRoute();
        List<Route> routeList = new ArrayList<>();
        for(Map.Entry<Integer, Route> map : hashMap.entrySet()){
            int count = 0;
            for(String stationName : list){
                if(isContain(map.getValue(), stationName)){
                    count++;
                }
            }
            if(count == list.size()){
                routeList.add(map.getValue());
            }
        }
        return routeList;
    }
    //判断一条路线中是否包含指定站点
    public boolean isContain(Route route, String stationName){
        for(String str : route.stationList){
            if(str.equals(stationName))
                return true;
        }
        return false;
    }
    //获取指定站点是该线路中第几个
    public int obtainStationIndex(String stationName){
        int index = 1;
        for(String str : stationList){
            if(!str.equals(stationName)){
                index++;
            }
            else{
                break;
            }
        }
        return index;
    }
}